<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class kelas extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->model('m_kelas');
        $this->load->model('m_silabus');
        //--
        $this->smarty->assign('kelas', $this->m_kelas);
        // load library
        $this->load->library('tnotification');
        $this->load->library('pagination');
    }

    // halaman list data
    public function index() {
        // set page rules
        $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "kuliah/kelas/index.html");
        // tahun ajaran
        $rs_ta = $this->m_kelas->get_detail_data_tahunajar();
        // params
        $params = array($rs_ta['tahun'], $rs_ta['semester'], $this->com_user['user_id']);
        // data
        $rs_id = $this->m_kelas->get_data_makul_diampu($params);
        $this->smarty->assign('rs_id', $rs_id);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // halaman pengaturan kelas baru
    public function manage($tahun = "", $semester = "", $dosen_kd = "", $makul_kd = "") {
        // set page rules
        $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "kuliah/kelas/manage.html");
        // load javascript
        $this->smarty->load_javascript('resource/js/jquery/jquery-ui-1.9.2.custom.min.js');
        // load css 
        $this->smarty->load_style('jquery.ui/redmond/jquery-ui-1.8.13.custom.css');
        // mata kuliah
        $this->smarty->assign('makul', $this->m_silabus->get_detail_makul($makul_kd));
        // jurusan
        $this->smarty->assign('jurusan', $this->m_silabus->get_data_jurusan());
        // params
        $this->smarty->assign('tahun', $tahun);
        $this->smarty->assign('semester', $semester);
        $this->smarty->assign('dosen_kd', $dosen_kd);
        $this->smarty->assign('makul_kd', $makul_kd);
        //--
        $params = array($semester, $tahun, $makul_kd, $dosen_kd);
        // data
        $rs_id = $this->m_kelas->get_data_kelas($params);
        $this->smarty->assign('rs_id', $rs_id);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses tambah kelas
    public function proses_tambah_kelas() {
        // set page rules
        $this->_set_page_rule("C");
        // params
        $tahun = $this->input->post('tahun');
        $semester = $this->input->post('semester');
        $dosen_kd = $this->input->post('dosen_kd');
        $makul_kd = $this->input->post('makul_kd');
        $jurusan_kd = $this->input->post('jurusan_kd');
        $kelas_nm = $this->input->post('kelas_nm');
        //--
        $params = array(
            $kelas_nm, $semester, $tahun, $makul_kd, $jurusan_kd, $dosen_kd, $this->com_user['user_id']
        );
        // insert
        $this->m_kelas->insert_data_kelas($params);
        $response = array(
            "tahun" => $tahun, "semester" => $semester,
            "dosen_kd" => $dosen_kd, "makul_kd" => $makul_kd
        );
        echo json_encode($response);
    }

    // halaman detail kelas
    public function detail($kelas_kd = "") {
        // set page rules
        $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "kuliah/kelas/detail.html");
        // info
        $rs_info = $this->m_kelas->get_detail_info_kelas($kelas_kd);
        $this->smarty->assign('rs_info', $rs_info);
        // data
        $rs_id = $this->m_kelas->get_data_item_kelas($kelas_kd);
        $this->smarty->assign('rs_id', $rs_id);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // halaman tambah detail kelas
    public function detail_add($kelas_kd = "") {
        // set page rules
        $this->_set_page_rule("C");
        // set template content
        $this->smarty->assign("template_content", "kuliah/kelas/detail_add.html");
        // load javascript
        $this->smarty->load_javascript('resource/js/jquery/jquery-ui-1.9.2.custom.min.js');
        // load css 
        $this->smarty->load_style('jquery.ui/redmond/jquery-ui-1.8.13.custom.css');
        // info
        $rs_info = $this->m_kelas->get_detail_info_kelas($kelas_kd);
        $this->smarty->assign('rs_info', $rs_info);
        // data materi
        $params = array($rs_info['semester'], $rs_info['tahun'], $rs_info['dosen_kd'], $rs_info['makul_kd'], $rs_info['jurusan_kd'], "teori");
        $rs_materi = $this->m_kelas->get_materi_kelas($params);
        $this->smarty->assign('rs_materi', $rs_materi);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses tambah detail kelas
    public function detail_add_process() {
        // set page rules
        $this->_set_page_rule("C");
        // cek input
        $this->tnotification->set_rules('kelas_kd', 'Kode Kelas', 'required');
        $this->tnotification->set_rules('kelas_minggu', 'Pertemuan', 'required|numeric|max_length[2]');
        $this->tnotification->set_rules('kelas_tgl', 'Tanggal', 'required');
        $this->tnotification->set_rules('silabus_detail_kd', 'Materi', 'required');
        // proses
        if ($this->tnotification->run() !== FALSE) {
            // params
            $params = array(
                $this->input->post('kelas_kd'), $this->input->post('kelas_tgl'),
                $this->input->post('kelas_minggu'), $this->input->post('silabus_detail_kd'),
                "open", $this->com_user['user_id']
            );
            //insert
            if ($this->m_kelas->insert_detail_kelas($params)) {
                $kelas_detail_kd = $this->m_kelas->get_last_inserted_id();
                // set status kelas
                $this->m_kelas->set_status_kelas(array('close', $this->input->post('kelas_kd'), $kelas_detail_kd));
                // sukses
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // redirect
        redirect('kuliah/kelas/detail_add/' . $this->input->post('kelas_kd'));
    }

    // halaman detail view kelas
    public function detail_view($kelas_kd = "", $kelas_detail_kd = "") {
        // set page rules
        $this->_set_page_rule("U");
        // set template content
        $this->smarty->assign("template_content", "kuliah/kelas/detail_view.html");
        // load javascript
        $this->smarty->load_javascript('resource/js/jquery/jquery-ui-1.9.2.custom.min.js');
        // load css 
        $this->smarty->load_style('jquery.ui/redmond/jquery-ui-1.8.13.custom.css');
        // info
        $rs_info = $this->m_kelas->get_detail_info_kelas($kelas_kd);
        $this->smarty->assign('rs_info', $rs_info);
        // data 
        $result = $this->m_kelas->get_detail_data_kelas($kelas_detail_kd);
        $this->smarty->assign('result', $result);
        // data materi
        $rs_materi = $this->m_kelas->get_sub_materi_kelas($result['silabus_detail_kd']);
        $this->smarty->assign('rs_materi', $rs_materi);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses update detail kelas
    public function detail_view_process() {
        // set page rules
        $this->_set_page_rule("U");
        // cek input
        $this->tnotification->set_rules('kelas_detail_kd', 'Kode Detail Kelas', 'required');
        $this->tnotification->set_rules('kelas_note', 'Catatan Perkuliahan');
        // proses
        if ($this->tnotification->run() !== FALSE) {
            // params
            $params = array(
                $this->input->post('kelas_note'), $this->input->post('save'),
                $this->com_user['user_id'], $this->input->post('kelas_detail_kd')
            );
            // update
            if ($this->m_kelas->update_detail_kelas($params)) {
                // sukses
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
                // redirect
                redirect('kuliah/kelas/detail/' . $this->input->post('kelas_kd'));
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // redirect
        redirect('kuliah/kelas/detail_view/' . $this->input->post('kelas_kd') . '/' . $this->input->post('kelas_detail_kd'));
    }

}